package com.totcy.atom.net.subscriber;

import android.util.Log;

import com.google.gson.stream.MalformedJsonException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.totcy.atom.CommonValues;
import com.totcy.atom.MyApplication;
import com.totcy.atom.net.callback.OnResultCallBack;
import com.totcy.atom.net.exception.ApiException;
import com.totcy.baselibrary.utils.NetUtil;

import java.io.EOFException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.CompositeException;

/**
 * description: Retrofit RxJava观察者，http请求响应处理
 * @author: Totcy
 * date: 2017/9/19 10:58
 * update: 2017/9/19
 */
public abstract class BaseHttpSubscriber<T> implements Observer<T> ,OnResultCallBack<T> {
    private Disposable mDisposable;

    public BaseHttpSubscriber() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        mDisposable = d;
    }

    @Override
    public void onNext(T t) {
        onSuccess(t);
    }

    @Override
    public void onError(Throwable e) {

        //这里可能发生多个异常 网络异常、缓存异常……
        if (e instanceof CompositeException) {
            CompositeException compositeE = (CompositeException) e;
            for (Throwable throwable : compositeE.getExceptions()) {
                dealWithException(throwable);
            }
        } else {
            //单个普通异常处理
            dealWithException(e);
        }
    }

    private void dealWithException(Throwable e) {
        if(!NetUtil.isConnected(MyApplication.getApplication())){
            onError(ApiException.Code_UnConnected, ApiException.CONNECT_EXCEPTION);
        } else if (e instanceof SocketTimeoutException) {
            onError(ApiException.Code_TimeOut, ApiException.SOCKET_TIMEOUT_EXCEPTION);
        } else if (e instanceof ConnectException) {
            onError(ApiException.Code_UnConnected, ApiException.CONNECT_EXCEPTION);
        } else if (e instanceof UnknownHostException) {
            onError(ApiException.Code_UnConnected, ApiException.CONNECT_EXCEPTION);
        } else if (e instanceof SocketException) {
            //可能是应用禁止联网了
            onError(ApiException.Code_UnConnected, ApiException.CONNECT_EXCEPTION);
        } else if (e instanceof HttpException) {
            //服务器端异常 可能是WiFi无网络
            onError(ApiException.Code_ServiceFail, ApiException.SERVER_EXCEPTION);
        } else if (e instanceof EOFException) {
            //服务器端异常
            onError(ApiException.Code_ServiceFail, ApiException.SERVER_EXCEPTION);
        } else if (e instanceof MalformedJsonException) {
            onError(ApiException.Code_MalformedJson, ApiException.MALFORMED_JSON_EXCEPTION);
        } else if (e instanceof ApiException) {
            //服务器返回的错误
            int errorCode = ((ApiException) e).getResultCode();
            //需要登录,这里所有接口拦截处理，但是也会回调 onError方法
            //……
            onError(((ApiException) e).getResultCode(), e.getMessage());
        } else {
            //未处理的显示服务器错误
            onError(ApiException.Code_Other, ApiException.SERVER_EXCEPTION);
            if (CommonValues.isRunningTest) {
                Log.e(BaseHttpSubscriber.class.getSimpleName(), "Http Exception(未知错误):" + e);
                return;
            }
        }
        if (CommonValues.isRunningTest) {
            Log.e(BaseHttpSubscriber.class.getSimpleName(), "Http Exception:" + e);
        }
    }

    @Override
    public void onComplete() {

    }

    public void unSubscribe() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
